<?php
namespace app\admin\model;

use think\Db;
use think\Model;

class ChannelReport extends Model {

	public function searchChannelIdAttr($query, $value, $data) {
		if ($value) {
			$query->where('cr.channel_id', $value);
		}
	}
	public function searchDateAttr($query, $value, $data) {
		if ($value) {
			$query->where('cr.date', '>=', $value . '-01')
				->where('cr.date', '<', date('Y-m-01', strtotime($value . '-01 +1 month')));
		}
	}

	public function getChannelReport($request) {

		$data = $this->alias('cr')->withSearch(['channel_id', 'date'], $request)
			->field('cr.*,cha.company_name')
			->join('bus_channel cha', 'cr.channel_id=cha.channel_id')
			->paginate()
			->toArray();

		return $data;
	}

	public function getChannelMonthReport($request) {
		$data = $this->alias('cr')->withSearch(['channel_id', 'date'], $request)
			->field('cr.channel_id,sum(cr.total) total,sum(cr.success) success,sum(cr.failed) failed,sum(cr.total_fee) total_fee,cha.company_name')
			->join('bus_channel cha', 'cr.channel_id=cha.channel_id')
			->group('cr.channel_id')
			->paginate()
			->toArray();

		return $data;
	}
}